import { _decorator, Component, Node, EventTouch, Vec2, Vec3 , UITransform, Sprite, Mat4, sp, Widget} from 'cc';
import { EventManager } from '../core/EventManager';
import { Databus } from '../core/Databus';
import { BilliardGame } from './BilliardGame';
import DataEventReport from '../platform/DataEventReport';

const { ccclass, property } = _decorator;
const databus = Databus.Instance();
@ccclass('SpinHitController')
export class SpinHitController extends Component {
  // 加塞点击球
  private spinClickBall: Node = null;
  // 加塞spin控制参数
  private touchSpinBall: Node = null;
  private crossLine: Node = null;
  private touchClickPoint: Node = null;
  private spinBackground: Node = null;
  private spinChooseClickPoint: Node = null;
  private spinBallRadius = 500;
  onLoad() {
    this.spinClickBall = this.node.getChildByName('SpinControlBall').getChildByName('SpinBallCenter').getChildByName('SpinBallSprite');

    this.touchSpinBall = this.node.getChildByName('SpinWhiteBall');
    this.crossLine = this.node.getChildByName('CrossLine');
    this.touchClickPoint = this.node.getChildByName('SpinClickPoint');
    this.spinBackground = this.node.getChildByName('SpinBackground');

    this.spinChooseClickPoint = this.node.getChildByName('SpinControlBall').getChildByName('SpinBallCenter').getChildByName('SpinChoosePoint');
    
    EventManager.Instance().AddEventListener('WhiteBallHitted', this, this.whiteBallHitted);
    
    this.onEnableEvent();
    this.hideSpin();
    this.aliginMargin();
  }
  aliginMargin() {
    const widget = this.node.getChildByName('SpinControlBall').getComponent(Widget);
    widget.right = databus.sceenSize.alignMargin();
  }
  update(deltaTime: number) {
    
  }

  onEnableEvent() {
    this.spinClickBall.on(Node.EventType.TOUCH_END, this.onSpinBallClick, this);
    this.touchSpinBall.on(Node.EventType.TOUCH_END, this.onSpinBallTouch, this);
    this.spinBackground.on(Node.EventType.TOUCH_END, this.onSpinBackgroundTouch, this);
  }
  onSpinBallClick(e: EventTouch) {
    if (this.touchSpinBall.active) {
      this.hideSpin();
      return;
    }
    this.showSpin();
  }

  onSpinBallTouch(e: EventTouch) {
    const loc: Vec2 = e.getUILocation();
    const uip = this.touchSpinBall.getComponent(UITransform);
    const ap = uip.convertToNodeSpaceAR(new Vec3(loc.x, loc.y, 0));
    if (!this.isInBallRound(ap.x, ap.y)) {
      this.hideSpin();
      return;
    }

    this.touchClickPoint.setPosition(new Vec3(ap.x, ap.y, 0));
    // 两个地方的球保持 8 倍关系
    this.spinChooseClickPoint.setPosition(new Vec3(ap.x / 8, ap.y / 8, 0));
    const spin = new Vec3(ap.x / this.spinBallRadius, ap.y / this.spinBallRadius, 0);

    BilliardGame.Instance().hasSpinHitBall = true;
    EventManager.Instance().Emit("ChangeOffsetSpinPosition", spin);

    DataEventReport.Instance().reportComponentEvent(
      databus.sceneStack.currentPageName(), 'SpinBallTouch'
    );
  }

  onSpinBackgroundTouch(e: EventTouch) {
    const loc: Vec2 = e.getUILocation();
    const uip = this.touchSpinBall.getComponent(UITransform);
    const ap = uip.convertToNodeSpaceAR(new Vec3(loc.x, loc.y, 0));
    if (this.isInBallRound(ap.x, ap.y)) {
      return;
    }
    this.hideSpin();
  }
  public whiteBallHitted(name: string, param: any) {
    this.spinChooseClickPoint.setPosition(Vec3.ZERO);
    this.touchClickPoint.setPosition(Vec3.ZERO);
  }
  showSpin() {
    this.touchSpinBall.active = true;
    this.touchClickPoint.active = true;
    this.spinBackground.active = true;
    this.crossLine.active = true;
  }
  hideSpin() {
    this.touchSpinBall.active = false;
    this.touchClickPoint.active = false;
    this.spinBackground.active = false;
    this.crossLine.active = false;
  }
  isInBallRound(x: number, y: number) {
    return x * x + y * y < this.spinBallRadius * this.spinBallRadius / 4;
  }
}

